#include "sysinfo.h"
#include "temperature.h"
#include <stdio.h>

float get_memRate()
{
    MEM_OCCUPY meminfo;
    MEM_OCCUPY *mem = &meminfo;
    memset(mem, 0x0, sizeof(MEM_OCCUPY));
    FILE* fp = fopen("/proc/meminfo","r");
    if(fp == NULL)
    {
        printf("Can not open file\r\n");
        return -1;
    }

    char buf[64];
    char name[32];
    memset(buf, 0x00, sizeof(buf));
    fgets(buf, sizeof(buf), fp);
    sscanf(buf, "%s %f %s", name, &mem->total, name);
    memset(buf, 0x00, sizeof(buf));
    fgets(buf, sizeof(buf), fp);
    sscanf(buf, "%s %f %s", name, &mem->free, name);
    //printf("buf is %s  name is %s %f\r\n", buf, name, &mem->free);

    float temp;
    sscanf(buf,"%s			%f %s", name, &temp, name);
    //printf("temp is %f \r\n", temp);
    float rate = (mem->total - mem->free)/mem->total;
    //printf("%.2f  %.2f	rate is %.2f%%\r\n", mem->total, mem->free, rate*100);
    fclose(fp);
    return rate;
}

float cal_cpuoccupy (CPU_OCCUPY *o, CPU_OCCUPY *n)
{
    unsigned long od, nd;
    unsigned long id, sd;
    float cpu_use = 0;

    od = (unsigned long) (o->user + o->nice + o->system +o->idle);//第一次(用户+优先级+系统+空闲)的时间再赋给od
    nd = (unsigned long) (n->user + n->nice + n->system +n->idle);//第二次(用户+优先级+系统+空闲)的时间再赋给od

    id = (unsigned long) (n->user - o->user);    //用户第一次和第二次的时间之差再赋给id
    sd = (unsigned long) (n->system - o->system);//系统第一次和第二次的时间之差再赋给sd
    if((nd-od) != 0)
        cpu_use = ((sd+id)*1.0)/(nd-od); //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
    else cpu_use = 0;
    //printf("cpu: %u/n",cpu_use);
    return cpu_use;
}

void get_cpuoccupy (CPU_OCCUPY *cpust) //对无类型get函数含有一个形参结构体类弄的指针O
{
    FILE *fd;
    char buff[256];
    CPU_OCCUPY *cpu_occupy;
    cpu_occupy=cpust;

    fd = fopen ("/proc/stat", "r");
    fgets (buff, sizeof(buff), fd);

    sscanf (buff, "%s %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,&cpu_occupy->system, &cpu_occupy->idle);

    fclose(fd);
}

SysInfo::SysInfo()
{
#ifndef __WIN32__
    get_cpuoccupy(&last_cpu_stat);
    timeID = startTimer(1000);
#endif
    temperature             = new Temperature;

    timerEvent(NULL);
}

SysInfo::~SysInfo()
{
    if(timeID != 0){
        killTimer(timeID);
    }
}

void SysInfo::timerEvent(QTimerEvent *)
{
    static unsigned long time_count = 0;

    time_count++;
    if(time_count % 3 == 1){
        cpu_temp = static_cast<float>(temperature->GetCpuTemperature() * 0.001);
        vdo_temp = temperature->GetVideoTemperature();
        hdd_temp = temperature->GetHddTemperature();
    }
    CPU_OCCUPY  cpu_stat;
    get_cpuoccupy(&cpu_stat);
    cpu_rate    = cal_cpuoccupy(&last_cpu_stat, &cpu_stat)*100;
    memcpy(&last_cpu_stat, &cpu_stat, sizeof(PACKED_CPU));
    mem_rate    = get_memRate()*100;
    if(time_count % 30 == 1)
        printf("cpu_rate: %.2f%%, mem_rate: %.2f%%, cpu_temp: %.2f, video_temp: %.2f, hdd_temp: %.2f\n",
               cpu_rate, mem_rate, cpu_temp, vdo_temp, hdd_temp);

    cpuRate = static_cast<int>(cpu_rate*10);
    memRate = static_cast<int>(mem_rate*10);
    cpuTemp = static_cast<int>(cpu_temp*10);
    vdoTemp = static_cast<int>(vdo_temp*10);
    hddTemp = static_cast<int>(hdd_temp*10);
}
